using System;
using System.Collections.Generic;
using System.Reflection;

namespace YunBox2.Contracts.Core
{
	public class EnumHelper
	{
		public static string GetDescription(Enum value)
		{
			if (value == null) throw new ArgumentNullException("value");
			string description = value.ToString();
			FieldInfo fieldInfo = value.GetType().GetField(description);
			EnumDescriptionAttribute[] attr = (EnumDescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(EnumDescriptionAttribute), false);
			if (attr != null && attr.Length > 0)
			{
				description = attr[0].Description;
			}

			return description;
		}

		public static IList<KeyValuePair<Enum, string>> ToList(Type type)
		{
			if (type == null) throw new ArgumentNullException("type");
			if (!type.IsEnum) throw new ArgumentException("Type provided must be an Enum.", "type");

			var list = new List<KeyValuePair<Enum, string>>();
			Array array = Enum.GetValues(type);
			foreach (Enum value in array)
			{
				list.Add(new KeyValuePair<Enum, string>(value, GetDescription(value)));
			}

			return list;
		} 
	}

	[AttributeUsage(AttributeTargets.Enum | AttributeTargets.Field, AllowMultiple = false)]
	public class EnumDescriptionAttribute : Attribute
	{
		private readonly string description;

		public EnumDescriptionAttribute(string description)
		{
			this.description = description;
		}

		public string Description
		{
			get
			{
				return description;
			}
		}
	}
}